<template>
  <div class="template-content">
    <div id="container" ref="container" style="width: 100%; height: 100%; background-color: #000" @dblclick="fullscreenSwich">
      <div v-show="loaded" class="buttons-box" ref="buttonsBox">
        <div>
          <i v-if="playing" class="iconfont icon-stop jessibuca-btn" title="停止" @click="destroy"></i>
          <i v-if="!playing" class="iconfont icon-play jessibuca-btn" title="暂停" @click="playBtnClick"></i>
          <i v-if="playing" class="iconfont icon-pause jessibuca-btn" title="播放" @click="pause"></i>
        </div>
        <div class="buttons-box-right">
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  name: 'JsPlayer',
  data() {
    return{
      btnDom:null,
      jsPlayer:null,
      vod:true,
      forceNoOffscreen: false,
      playing:false,
      loaded:false,
    }
  },
  methods:{
    /**
     * 创建播放器
     * @param container
     * @param videoUrl
     * @returns {Jessibuca}
     */
    createPlayer(){
      let options =  {};
      let jplayer = new window.Jessibuca(Object.assign(
        {
          container: this.$refs.container,
          videoBuffer: 0.1, // 最大缓冲时长，单位秒
          isResize: false,
          isFlv: true,
          decoder: process.env.BASE_URL + "jessibuca/index.js",
          text: "",//"TN-28181",
          // background: "bg.jpg",
          loadingText: "加载中",
          hasAudio: false,//typeof (this.hasAudio) =="undefined"? true: this.hasAudio,
          debug: false,
          supportDblclickFullscreen: false, // 是否支持屏幕的双击事件，触发全屏，取消全屏事件。
          operateBtns: {
            fullscreen: false,
            screenshot: false,
            play: false,
            audio: false,
          },
          record: "record",
          vod: this.vod,
          forceNoOffscreen: false,//this.forceNoOffscreen,
          isNotMute: false,//this.isNotMute,
        },
        options
      ));
      jplayer.on("load", function () {
        console.log("on load init");
      });

      jplayer.on("log", function (msg) {
        console.log("on log", msg);
      });
      jplayer.on("record", function (msg) {
        console.log("on record:", msg);
      });
      jplayer.on("pause", function () {
        console.log("on pause ");
      });
      jplayer.on("play", function () {
        //_this.playing = true;
      });
      jplayer.on("fullscreen", function (msg) {
        console.log("on fullscreen", msg);
        //_this.fullscreen = msg
      });

      jplayer.on("mute", function (msg) {
        console.log("on mute", msg);
        //_this.isNotMute = !msg;
      });
      jplayer.on("audioInfo", function (msg) {
        console.log("audioInfo", msg);
      });

      jplayer.on("videoInfo", function (msg) {
        //this.videoInfo = msg;
        console.log("videoInfo", msg);

      });

      jplayer.on("bps", function (bps) {
        console.log('bps', bps);

      });
      let _ts = 0;
      jplayer.on("timeUpdate", function (ts) {
        // console.log('timeUpdate,old,new,timestamp', _ts, ts, ts - _ts);
        _ts = ts;
      });
      jplayer.on("error", function (error) {
        console.log("error", error);
      });

      jplayer.on("timeout", function () {
        console.log("timeout");
      });

      jplayer.on('start', function () {
        console.log('start');
      })

      jplayer.on("performance", function (performance) {
        let show = "卡顿";
        if (performance === 2) {
          show = "非常流畅";
        } else if (performance === 1) {
          show = "流畅";
        }
        //_this.performance = show;
      });
      jplayer.on('buffer', function (buffer) {
        //console.log('buffer', buffer);
      })

      jplayer.on('stats', function (stats) {
        //console.log('stats', stats);
      })

      jplayer.on('kBps', function (kBps) {
        //_this.kBps = Math.round(kBps);
      });

      // 显示时间戳 PTS
      jplayer.on('videoFrame', function () {

      })

      //
      jplayer.on('metadata', function () {

      });

      return jplayer;
    },
    play(videoUrl){
      //console.log("Play时的地址为: " + this.videoUrl)
      if (!!this.jsPlayer) {
        this.destroy();
      }
      this.jsPlayer=this.createPlayer();
      this.jsPlayer.on("play", () => {
        this.playing = true;
        this.loaded = true;
        this.quieting = false;//this.jsPlayer.quieting;
      });
      if (this.jsPlayer.hasLoaded()) {
        this.jsPlayer.play(videoUrl);
      } else {
        this.jsPlayer.on("load", () => {
          console.log("load 播放")
          this.jsPlayer.play(videoUrl);
        });
      }
    },
    /**
     * 停止
     */
    destroy: function () {
      console.log("*****player destroy*******");
      if (!!this.jsPlayer) {
        this.jsPlayer.destroy();
        this.jsPlayer = null;
      }
      if (!this.$refs.buttonsBox){
        this.$refs.container.appendChild(this.btnDom);
      }

/*      if (document.getElementById("buttonsBox") == null) {
        document.getElementById("container").appendChild(this.btnDom)
      }*/
      this.jsPlayer = null;
      this.playing = false;
/*      this.err = "";
      this.performance = "";*/

    },
    stop: function (){
      this.destroy();
    },
    playBtnClick: function (event){
      this.play(this.videoUrl)
    },

    /**
     * 暂停
     */
    pause: function () {
      if (!!this.jsPlayer) {
        this.jsPlayer.pause();
      }
      this.playing = false;
/*      this.err = "";
      this.performance = "";*/
    },
    fullscreenSwich: function (){
/*      let isFull = this.isFullscreen()
      this.jsPlayer.setFullscreen(!isFull)
      this.fullscreen = !isFull;*/
    },
/*    isFullscreen: function (){
      return document.fullscreenElement ||
        document.msFullscreenElement  ||
        document.mozFullScreenElement ||
        document.webkitFullscreenElement || false;
    }*/

  },
  mounted() {
    //this.btnDom = document.getElementById("buttonsBox");
    this.btnDom = this.$refs.buttonsBox;
  },
  beforeDestroy(){

    if (this.jsPlayer ){
      this.jsPlayer.destroy();
      this.jsPlayer=null;
    }
    this.playing = false;
    this.loaded = false;
  },

  destroyed() {},
}
</script>
<style>
@import "../../template.less";
.buttons-box{
  width: 100%;
  height: 28px;
  background-color: rgba(43, 51, 63, 0.7);
  position: absolute;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  left: 0;
  bottom: 0;
  user-select: none;
  z-index: 10;
}
.buttons-box-right {
  position: absolute;
  right: 0;
}

</style>
